import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/store.js'

Vue.use(Router)
if (sessionStorage.getItem('token')) {
	store.commit('set_token', sessionStorage.getItem('token'))
}
const router = new Router({
	mode: "history",
	routes: [{
		path: '/',
		redirect: '/dashboard'
	},

	{
		path: '/',
		name: 'home',
		meta: {
			requireAuth: true
		},
		component: resolve => require(['../components/base/Home.vue'], resolve),
		children: [{
			path: '/dashboard',
			component: resolve => require(['../components/base/Dashboard.vue'], resolve),
			meta: {
				title: '系统首页',
				requireAuth: true
			}
		},
		{
			path: '/user',
			component: resolve => require(['../components/userManage/UserList.vue'], resolve),
			meta: {
				title: '用户管理',
				requireAuth: true
			}
		},
		{
			path: '/role',
			component: resolve => require(['../components/userManage/SysRole.vue'], resolve),
			meta: {
				title: '角色管理',
				requireAuth: true
			}
		},
		{
			path: '/source',
			component: resolve => require(['../components/userManage/SysResource.vue'], resolve),
			meta: {
				title: '资源管理',
				requireAuth: true
			}
		},
		{
			path: '/icon',
			component: resolve => require(['../components/page/Icon.vue'], resolve),
			meta: {
				title: '自定义图标',
				requireAuth: true
			}
		},
		{
			path: '/table',
			component: resolve => require(['../components/page/BaseTable.vue'], resolve),
			meta: {
				title: '基础表格',
				requireAuth: true
			}
		},
		{
			path: '/tabs',
			component: resolve => require(['../components/page/Tabs.vue'], resolve),
			meta: {
				title: 'tab选项卡',
				requireAuth: true
			}
		},
		{
			path: '/form',
			component: resolve => require(['../components/page/BaseForm.vue'], resolve),
			meta: {
				title: '基本表单',
				requireAuth: true
			}
		},
		{
			// 富文本编辑器组件
			path: '/editor',
			component: resolve => require(['../components/page/VueEditor.vue'], resolve),
			meta: {
				title: '富文本编辑器',
				requireAuth: true
			}
		},
		{
			// markdown组件
			path: '/markdown',
			component: resolve => require(['../components/page/Markdown.vue'], resolve),
			meta: {
				title: 'markdown编辑器',
				requireAuth: true
			}
		},
		{
			// 图片上传组件
			path: '/upload',
			component: resolve => require(['../components/page/Upload.vue'], resolve),
			meta: {
				title: '文件上传',
				requireAuth: true
			}
		},
		{
			// vue-schart组件
			path: '/charts',
			component: resolve => require(['../components/page/BaseCharts.vue'], resolve),
			meta: {
				title: 'schart图表',
				requireAuth: true
			}
		},
		{
			// 拖拽列表组件
			path: '/drag',
			component: resolve => require(['../components/page/DragList.vue'], resolve),
			meta: {
				title: '拖拽列表',
				requireAuth: true
			}
		},
		{
			// 拖拽Dialog组件
			path: '/dialog',
			component: resolve => require(['../components/page/DragDialog.vue'], resolve),
			meta: {
				title: '拖拽弹框',
				requireAuth: true
			}
		},
		{
			// 国际化组件
			path: '/i18n',
			component: resolve => require(['../components/page/I18n.vue'], resolve),
			meta: {
				title: '国际化',
				requireAuth: true
			}
		},
		{
			// 权限页面
			path: '/permission',
			component: resolve => require(['../components/page/Permission.vue'], resolve),
			meta: {
				title: '权限测试',
				requireAuth: true
			}
		},
		{
			path: '/404',
			component: resolve => require(['../components/page/404.vue'], resolve),
			meta: {
				title: '404'
			}
		},
		{
			path: '/403',
			component: resolve => require(['../components/page/403.vue'], resolve),
			meta: {
				title: '403'
			}
		},
		{
			path: '/donate',
			component: resolve => require(['../components/page/Donate.vue'], resolve),
			meta: {
				title: '支持作者'
			}
		}
		]
	},
	{
		path: '/login',
		name: 'login',
		component: resolve => require(['../components/Login.vue'], resolve)
	},
	{
		path: '*',
		redirect: '/404'
	}
	]
})
//跳转路由前判断是否登录 to.matched.some(r => r.meta.requireAuth)
router.beforeEach((to, from, next) => {
	if (to.matched.some(r => r.meta.requireAuth)) { //这里的requireAuth为路由中定义的 meta:{requireAuth:true}，意思为：该路由添加该字段，表示进入该路由需要登陆的
		if (store.state.token) {
			next();
		} else {
			console.log("未登录！")
			next({
				path: '/login',
				query: {
					redirect: to.fullPath
				}
			})
		}
	} else {
		next();
	}
})
export default router
